home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Shaders / RCShaders / RCDFerrule.sl < prev    next >
Encoding:
Text File  |  1995-03-22  |  1.5 KB  |  47 lines

  1. /* Listing 16.35  Ferrule displacement shader for a pencil*/
  2. /*
  3.  * dferrule(): Deform the cylinder that models the ferrule of a pencil.
  4.  *  The shader adds four ridges that circumscribe the cylinder and  
  5.  *  many more between the innermost ridges that are parallel to 
  6.  *  the cylinder's axis. The ridges are displaced sine waves.
  7.  *  The ferrule is also slightly flared at the eraser end. 
  8.  */
  9. #define UFREQ 219.911
  10. #define VFREQ 104.72
  11. #define R1MIN .17
  12. #define R1MAX .23
  13. #define R2MIN .31
  14. #define R2MAX .37
  15. #define R3MIN .63
  16. #define R3MAX .69
  17. #define R4MIN .77
  18. #define R4MAX .83
  19. displacement
  20. RCDFerrule(
  21.     float    Km = .005 )
  22. {
  23.     float    magnitude = 0;
  24.  
  25.     /* Compute the distance the surface should be displaced. */
  26.     if (v <= .02)         /* the eraser-end flair */
  27.         magnitude = 2*(1 - sin(78.54*v));
  28.  
  29.     if ((v >= R1MIN) && (v <= R1MAX))                             /* first circular ridge */
  30.         magnitude = 3*(1 - cos(VFREQ*(v-R1MIN)));
  31.     else if ((v >= R2MIN) && (v <= R2MAX))                            /* second circular ridge */
  32.         magnitude = 3*(1 - cos(VFREQ*(v-R2MIN)));
  33.     else if ((v >.37) && (v < .63))                             /* the longitudinal ridges */
  34.         magnitude = -sin(UFREQ*u) - 1;
  35.     else if ((v >= R3MIN) && (v <= R3MAX))                            /* third circular ridge */
  36.         magnitude = 3*(1 - cos(VFREQ*(v-R3MIN)));
  37.     else if ((v >= R4MIN) && (v <= R4MAX))                            /* fourth circular ridge */
  38.         magnitude = 3*(1 - cos(VFREQ*(v-R4MIN)));
  39.  
  40.     /* Now apply the displacement in the direction of the normal. */
  41.     P += Km * magnitude * normalize(N);
  42.  
  43.     /* Recalculate the normal. */
  44.     N = calculatenormal(P);
  45. }
  46.  
  47.